package com.dqhl.business.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;

import com.dqhl.business.R;
import com.dqhl.business.util.ScreenUtils;

/**
 * Created by guanluocang on 2016/11/4.
 */
public class GoodsManagerOperationDownPopWindow extends PopupWindow {

    private Activity context;
    private OnOperationListener onOperationListener;

    /**
     * @param context
     * @param open                禁用状态:1启用,2禁用
     * @param onOperationListener
     */
    public GoodsManagerOperationDownPopWindow(Activity context, String open,
                                              final OnOperationListener onOperationListener) {
        super(context);
        this.context = context;
        this.onOperationListener = onOperationListener;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.layout_goodsmanagerpopwindow_select, null);
        Button btn_edit = (Button) view.findViewById(R.id.btn_edit);
        Button btn_upGoods = (Button) view.findViewById(R.id.btn_upGoods);
        Button btn_forbid = (Button) view.findViewById(R.id.btn_forbid);
        Button btn_cancel = (Button) view.findViewById(R.id.btn_cancel);

        if ("1".equals(open)) {
            btn_forbid.setText("禁用");
        } else {
            btn_forbid.setText("启用");
        }

        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onOperationListener.onCancelGoods();
            }
        });
        btn_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onOperationListener.OnEditGoods();
            }
        });
        btn_upGoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onOperationListener.OnUpGoods();
            }
        });
        btn_forbid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onOperationListener.onForbidGoods();
            }
        });

        int width = (int) (ScreenUtils.getScreenWidth(context) * 0.9);
        setWidth(width);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setBackgroundDrawable(new ColorDrawable(0));
        setFocusable(true);
        setOutsideTouchable(true);
        setContentView(view);
        update();
    }

    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0+
        context.getWindow().setAttributes(lp);
    }

    public interface OnOperationListener {
        void OnEditGoods();

        void OnUpGoods();

        void onForbidGoods();

        void onCancelGoods();
    }
}
